package com.njcky.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

//import static com.yutu.garden.constant.SmxConstant.*;
import static com.njcky.constant.SmxConstant.*;

/**
 * @Author: lcy
 * @Date: 2023/7/3 18:51
 * @Desc:
 */
@Configuration
@EnableAsync
public class ThreadPoolConfig {
    /**
     * 记录
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(ThreadPoolConfig.class);

    /**
     * 配置线程池
     * @return
     */
    @Bean
    public ThreadPoolTaskExecutor threadPoolExecutor() {
        LOGGER.info("start asyncServiceExecutor");
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //配置核心线程数
        executor.setCorePoolSize(CORE_POOL_SIZE);
        //配置最大线程数SmxConstant
        executor.setMaxPoolSize(MAX_POOL_SIZE);
        //配置队列大小
        executor.setQueueCapacity(QUEUE_CAPACITY);
        //配置线程池中的线程的名称前缀
        executor.setThreadNamePrefix(NAME_PREFIX);
        //超时时间
        executor.setKeepAliveSeconds(ALIVE_SECONDS);
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //执行初始化
        executor.initialize();
        return executor;
    }
}
